package main

import (
	"context"
	"fmt"
	"github.com/go-kit/kit/log"
	endpoint "http_rest/endpoint"
	"http_rest/middleware"
	service "http_rest/service"
	"http_rest/transport"
	"net/http"
	"os"
	"os/signal"
	"syscall"
)
//command
//curl '127.0.0.1:9000/calculate/add/1/1' -X POST
//curl '127.0.0.1:9000/calculate/multiply/10/10' -X POST
func main()  {
	ctx := context.Background()
	errChan := make(chan error)

	var logger log.Logger
	{
		logger = log.NewLogfmtLogger(os.Stderr)
		logger = log.With(logger, "ts", log.DefaultTimestampUTC)
		logger = log.With(logger, "caller", log.DefaultCaller)
	}

	var svc service.Service
	svc = service.ArithmeticService{}
	svc = middleware.LoggingMiddleware(logger)(svc)
	ep := endpoint.MakeArithmeticEndpoint(svc)

	r := transport.MakeHttpHandler(ctx, ep, logger)

	go func() {
		fmt.Println("Http Server start at port:9000")
		handler := r
		errChan <- http.ListenAndServe(":9000", handler)
	}()

	go func() {
		c := make(chan os.Signal, 1)
		signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
		errChan <- fmt.Errorf("%s", <-c)
	}()

	fmt.Println(<-errChan)
}
